package com.haozi.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.haozi.reggie.common.BaseContext;
import com.haozi.reggie.common.CustomException;
import com.haozi.reggie.common.R;
import com.haozi.reggie.entity.AddressBook;
import com.haozi.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 获取地址列表.
     * @return
     */
    @GetMapping("/list")
    public R<List> list(HttpSession session) {
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Long user = (Long) session.getAttribute("user");
        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId,user)
                .orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(addressBookLambdaQueryWrapper);
        return R.success(list);
    }

    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook) {
        //获取联系人
        String consignee = addressBook.getConsignee();
        //获取手机号
        String phone = addressBook.getPhone();
        //获取收货地址
        String detail = addressBook.getDetail();
        if(consignee == null || phone == null || detail == null) {
            return R.error("请输入完整");
        }
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getConsignee,consignee)
                .eq(AddressBook::getPhone,phone)
                .eq(AddressBook::getDetail,detail);
        //查询相同的地址是否已经存在
        int count = addressBookService.count(addressBookLambdaQueryWrapper);
        if(count > 0) {
            throw new CustomException("相同的信息已经存在");
        }
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("新增地址成功");
    }

    /**
     * 获取地址详细信息
     * @param id
     * @return
     */
    @GetMapping({"/{id}"})
    public R<AddressBook> get(@PathVariable Long id) {
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getId,id);
        AddressBook addressBook = addressBookService.getOne(addressBookLambdaQueryWrapper);
        if(addressBook == null) {
            return R.error("地址不存在");
        }
        return R.success(addressBook);
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam Long ids){
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }

    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return R.success("更新成功");
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<String> defaults(@RequestBody AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        updateWrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(updateWrapper);

        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置成功");
    }

    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault() {
        //获取用户id
        Long currentId = BaseContext.getCurrentId();
        System.out.println(currentId);
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId,currentId);
        addressBookLambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(addressBookLambdaQueryWrapper);
        System.out.println(addressBook);
        return R.success(addressBook);
    }



}
